#!/bin/sh /etc/rc.common

START=99
STOP=15
USE_PROCD=1
PROG="/usr/sbin/openwisp_config"
PROG_NAME="OpenWISP config agent"

start_service() {
	config_load openwisp
	url=$(config_get http url)
	interval=$(config_get http interval)
	verify_ssl=$(config_get http verify_ssl)
	uuid=$(config_get http uuid)
	key=$(config_get http key)
	shared_secret=$(config_get http shared_secret)
	consistent_key=$(config_get http consistent_key)
	hardware_id_script=$(config_get http hardware_id_script)
	hardware_id_key=$(config_get http hardware_id_key)
	bootup_delay=$(config_get http bootup_delay)
	unmanaged=$(config_get http unmanaged)
	merge_config=$(config_get http merge_config)
	test_config=$(config_get http test_config)
	test_script=$(config_get http test_script)
	connect_timeout=$(config_get http connect_timeout)
	max_time=$(config_get http max_time)
	capath=$(config_get http capath)
	cacert=$(config_get http cacert)
	mac_interface=$(config_get http mac_interface)
	management_interface=$(config_get http management_interface)
	pre_reload_hook=$(config_get http pre_reload_hook)
	post_reload_hook=$(config_get http post_reload_hook)
	post_registration_hook=$(config_get http post_registration_hook)
	if [ $url ]; then url="--url $url"; fi
	if [ $interval ]; then interval="--interval $interval"; fi
	if [ $verify_ssl ]; then verify_ssl="--verify-ssl $verify_ssl"; fi
	if [ $uuid ]; then uuid="--uuid $uuid"; fi
	if [ $key ]; then key="--key $key"; fi
	if [ $shared_secret ]; then shared_secret="--shared-secret $shared_secret"; fi
	if [ $consistent_key ]; then consistent_key="--consistent-key $consistent_key"; fi
	if [ $hardware_id_script ]; then hardware_id_script="--hardware-id-script $hardware_id_script"; fi
	if [ $hardware_id_key ]; then hardware_id_key="--hardware-id-key $hardware_id_key"; fi
	if [ $bootup_delay ]; then bootup_delay="--bootup-delay $bootup_delay"; fi
	if [ -n "$unmanaged" ]; then
		# replace spaces with commas to avoid problems when
		# passing this arg to procd_set_param command
		unmanaged=$(echo $unmanaged | tr ' ' ',')
		unmanaged="--unmanaged $unmanaged";
	fi
	if [ $merge_config ]; then merge_config="--merge-config $merge_config"; fi
	if [ $test_config ]; then test_config="--test-config $test_config"; fi
	if [ $test_script ]; then test_script="--test-script $test_script"; fi
	if [ $connect_timeout ]; then connect_timeout="--connect-timeout $connect_timeout"; fi
	if [ $max_time ]; then max_time="--max-time $max_time"; fi
	if [ $capath ]; then capath="--capath $capath"; fi
	if [ $cacert ]; then cacert="--cacert $cacert"; fi
	if [ $mac_interface ]; then mac_interface="--mac-interface $mac_interface"; fi
	if [ $management_interface ]; then management_interface="--management-interface $management_interface"; fi
	if [ $pre_reload_hook ]; then pre_reload_hook="--pre-reload-hook $pre_reload_hook"; fi
	if [ $post_reload_hook ]; then post_reload_hook="--post-reload-hook $post_reload_hook"; fi
	if [ $post_registration_hook ]; then post_registration_hook="--post-registration-hook $post_registration_hook"; fi

	if [ -z "$url" ]; then
		logger -s "url is not set, please add it in /etc/config/openwisp" \
		       -t openwisp \
		       -p daemon.err
		exit 1
	fi

	if ([ -z "$uuid" ] || [ -z "$key" ]) && [ -z "$shared_secret" ]; then
		logger -s "you must either set uuid and key, or shared_secret in /etc/config/openwisp" \
		       -t openwisp \
		       -p daemon.err
		exit 1
	fi

	procd_open_instance
	procd_set_param command $PROG $url $interval $verify_ssl $uuid $key $shared_secret \
	                        $consistent_key $hardware_id_script $hardware_id_key \
	                        $bootup_delay $unmanaged $merge_config $test_config \
	                        $test_script $connect_timeout $max_time $capath $cacert \
	                        $mac_interface $management_interface $pre_reload_hook \
	                        $post_reload_hook $post_registration_hook
	procd_set_param respawn
	procd_close_instance
	logger -s "$PROG_NAME started" \
	       -t openwisp \
	       -p daemon.info
}

service_triggers() {
	procd_add_reload_trigger openwisp
}

stop_service() {
	logger -s "$PROG_NAME stopping" \
	       -t openwisp \
	       -p daemon.info
}

reload_service() {
	logger -s "$PROG_NAME received reload trigger" \
		   -t openwisp \
		   -p daemon.info
	local control_file=/tmp/openwisp/applying_conf
	# avoid reloading while configuration is being applied
	# will wait for a maximum of 30 seconds
	for i in $(seq 1 30); do
		if [ -f $control_file ]; then
			sleep 1
		else
			break
		fi
	done
	# ensure control file is removed
	rm $control_file
	# reload
    start
}
